﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Threading;

namespace Light
{
    /// <summary>
    /// TrafficLight绘图相关部分
    /// </summary>
    partial class TrafficLight : UIElement
    {

        Image light = new Image();
        Image cloud = new Image();

        Image timeunit = new Image();
        Image tenstimeunit = new Image();
        StackPanel time = new StackPanel();

        int state = 0;
       public int remainedTime = 0;

        private void dispatcherTimer_Tick( object sender, EventArgs e )
        {
            if ( state % 2 == 0 )           // 全黑
            {
                switch ( _Status )
                {
                    case LightStatus.LEFT_FLASH:
                    case LightStatus.STRAIGHT_FLASH:
                    case LightStatus.YELLOW_FLASH:
                        light.Source = new BitmapImage( new Uri( @"Resources/全黑.png", UriKind.Relative ) );
                        break;
                }
            }
            else
            {
                switch ( _Status )
                {
                    case LightStatus.LEFT_FLASH:
                        light.Source = new BitmapImage( new Uri( @"Resources/左转.png", UriKind.Relative ) );
                        break;
                    case LightStatus.STRAIGHT_FLASH:
                        light.Source = new BitmapImage( new Uri( @"Resources/直行.png", UriKind.Relative ) );
                        break;
                    case LightStatus.YELLOW_FLASH:
                        light.Source = new BitmapImage( new Uri( @"Resources/黄灯.png", UriKind.Relative ) );
                        break;
                }

                //SetTime( remainedTime - 1 );
            }

            state++;
        }

        public void LoadImage( int angle )
        {
            light.Source = new BitmapImage( new Uri( @"Resources/全黑.png", UriKind.Relative ) );
            light.Width = 90;
            light.Height = 30;

            SetTime( 1 );
            parentCanvas.Children.Add( light );
            Canvas.SetZIndex( light, 10 );

            cloud.Source = new BitmapImage( new Uri( @"Resources/cloud.png", UriKind.Relative ) );
            cloud.Width = 150;
            cloud.Height = 150;

            TranslateTransform tran = new TranslateTransform();
            tran.X = -25;
            tran.Y = -35;
            cloud.RenderTransform = tran;
            cloud.Opacity = 0.6;

            parentCanvas.Children.Add( cloud );


            // 打开计时器
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler( dispatcherTimer_Tick );
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds( 500 );
            dispatcherTimer.Start();
        }

        // 设置交通灯的剩余时间
        public void SetTime( int t )
        {
            if ( t <= 0 )
            {
                OnTimeZero();
                return;
            }

            remainedTime = t;
            int tensunit = t / 10;
            int unit = t - ( t/10 )*10;
            string strunit = "Resources\\";
            string strtensunit = "Resources\\";

            strunit += unit.ToString();
            strtensunit += tensunit.ToString();
            strunit += ".png";
            strtensunit += ".png";
            timeunit.Source = new BitmapImage( new Uri( strunit, UriKind.Relative ) );
            timeunit.Height = timeunit.Width = 50;
            tenstimeunit.Source = new BitmapImage( new Uri( strtensunit, UriKind.Relative ) );
            tenstimeunit.Height = tenstimeunit.Width = 50;

            time.Children.Clear();
            time.Children.Add( tenstimeunit );
            time.Children.Add( timeunit );

            parentCanvas.Children.Remove( time );
            parentCanvas.Children.Add( time );
            Canvas.SetZIndex( time, 9 );
        }

        void UpdateDraw()
        {
            lock ( road )
            {
                switch ( _Status )
                {
                    case LightStatus.LEFT:
                        light.Source = new BitmapImage( new Uri( @"Resources/左转.png", UriKind.Relative ) );
                        break;
                    case LightStatus.LEFT_FLASH:
                        light.Source = new BitmapImage( new Uri( @"Resources/左转.png", UriKind.Relative ) );
                        break;
                    case LightStatus.STRAIGHT:
                        light.Source = new BitmapImage( new Uri( @"Resources/直行.png", UriKind.Relative ) );
                        break;
                    case LightStatus.STRAIGHT_FLASH:
                        light.Source = new BitmapImage( new Uri( @"Resources/直行.png", UriKind.Relative ) );
                        break;
                    case LightStatus.RED:
                        light.Source = new BitmapImage( new Uri( @"Resources/红灯.png", UriKind.Relative ) );
                        break;
                    case LightStatus.YELLOW:
                        light.Source = new BitmapImage( new Uri( @"Resources/黄灯.png", UriKind.Relative ) );
                        break;
                    case LightStatus.YELLOW_FLASH:
                        light.Source = new BitmapImage( new Uri( @"Resources/黄灯.png", UriKind.Relative ) );
                        break;
                    case LightStatus.OFF:
                        light.Source = new BitmapImage( new Uri( @"Resources/全黑.png", UriKind.Relative ) );
                        break;
                }
            }

        }

        public override void OnUpdate()
        {
            Canvas.SetLeft( light, X );
            Canvas.SetTop( light, Y );
            Canvas.SetLeft( time, X );
            Canvas.SetTop( time, Y );
            Canvas.SetLeft( cloud, X );
            Canvas.SetTop( cloud, Y );
        }
    }
}
